#ifndef __SERVER_H__
#define __SERVER_H__

#include <soar.h>

__BEGIN_DECLS

struct cnfg_info
{/*{{{*/
    unsigned int ipckey;
    char errlog_location[LONG_FILE_NM_SIZE+1];
    unsigned int server_port;
    int threads;
    struct
    {
        struct _cnfg_svcnode
        {
            char name[NAME_SIZE+1];
            char addr[LONG_FILE_NM_SIZE+1];
            unsigned int mode;
        } *svcnode;
        int num;
    } _svcnode_;
};/*}}}*/
extern struct cnfg_info cnfg_info;

struct stat_info
{/*{{{*/
    struct
    {/*{{{*/
        pthread_mutex_t lock;
        struct _worker_info
        {
            int conn_num;
            unsigned int client_count;
            unsigned int client_call;
        } *worker;
    }/*}}}*/
    _worker_;
};/*}}}*/
extern struct stat_info stat_info;

enum thrd_state { THRD_RUNNING, THRD_SHUTDOWN };
struct operate_info
{/*{{{*/
    struct
    {
        pthread_mutex_t lock;
        unsigned int quit;
        enum thrd_state state;
    }
    _listener_;

    struct
    {
        pthread_mutex_t lock;
        unsigned int quit;
        enum thrd_state *state;
    }
    _worker_;
};/*}}}*/
extern struct operate_info operate_info;

#if 0
/*----- 共享信息 -----*/
#define WSCLIENT_DEF_NUM            3
#define WSCLIENT_MAX_NUM            200

enum _STAT { _RUNNING_, _SHUTDOWN_ };

struct share_info
{/*{{{*/
    UINT server_port;
    UINT monitor_port;

    struct
    {
        pthread_mutex_t lock;
        enum _STAT stat;
        UINT quit;
    } _listener_;

    struct
    {
        int num;
        pthread_mutex_t lock;
        struct _worker_info
        {
            enum _STAT stat;
            int conn_num;
        } *info;
        UINT quit;
    } _worker_;
};/*}}}*/
extern struct share_info *share_info;
extern UINT tracking;

/*----- monitor -----*/
#define MONITOR_IDLE_TIMEOUT    SECOND_OF_MINUTE(5) /* 监控闲置超时 */

struct monitor_ctl
{/*{{{*/
    SKT skt;
    char *msg_in;
    char *msg_out;
};/*}}}*/
extern int thrd_monitor(struct monitor_ctl *monitor_ctl);

/*----- worker -----*/
#define _SVC_TRACK_NUM          20

struct worker_ctl
{/*{{{*/
    UINT thrd_id;
    int worker_idx;
    UINT seq;
    EP ep;
    EP_SLOT listener_slot;
    char *msg_in;
    UINT tux_connected;

    struct _svc_track
    {
        struct
        {
            char name[NAME_SIZE+1];
            char begin_ts[TIMESTAMP_SIZE+1];
            char end_ts[TIMESTAMP_SIZE+1];
        } array[_SVC_TRACK_NUM];
        int svc_num;
        time_t begin_ts;
        char begin_ts_s[TIMESTAMP_SIZE+1];
        time_t end_ts;
        char end_ts_s[TIMESTAMP_SIZE+1];
    } svc_track;
};/*}}}*/
extern void thrd_worker(struct worker_ctl *worker_ctl);

/*----- listener -----*/
struct listener_ctl
{/*{{{*/
    UINT thrd_id;
    EP ep;
    EP_SLOT listener_slot;
    EP_SLOT worker_slot[WSCLIENT_MAX_NUM];
};/*}}}*/
extern void thrd_listener(struct listener_ctl *listener_ctl);

/*---- 内部命令 -----*/
#define INSVC_NEW_CONN              "0001"  /* 新连接 */
#endif

__END_DECLS

#endif
